<template>
  <transition name="el-zoom-in-center">
    <div class="JADP-preview-main">
      <div class="JADP-common-page-header">
        <el-page-header
          @back="goBack"
          :content="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
        />
        <div class="options">
          <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">
            保 存</el-button
          >
          <el-button @click="goBack"> 取 消</el-button>
        </div>
      </div>
      <el-row
        :gutter="15"
        class="word-form main"
        :style="{ margin: '0 auto', width: '100%' }"
      >
        <el-form
          ref="elForm"
          :model="dataForm"
          :rules="rules"
          size="medium"
          label-width="200px"
          label-position="right"
        >
          <template v-if="!loading">
            <el-col :span="24">
              <el-form-item label="头像" prop="photo">
                <JADP-UploadImg
                  v-model="dataForm.photo"
                  :fileSize="10"
                  :showTip="true"
                  sizeUnit="MB"
                  :limit="1"
                  buttonText="图片上传"
                >
                </JADP-UploadImg>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="姓名" prop="realname">
                <el-input
                  v-model="dataForm.realname"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性别" prop="gender">
                <el-select
                  v-model="dataForm.gender"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <el-option
                    v-for="(item, index) in genderOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="出生年月" prop="birthday">
                <el-date-picker
                  v-model="dataForm.birthday"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="timestamp"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="政治面貌" prop="politicallandscape">
                <el-input
                  v-model="dataForm.politicallandscape"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="身份证号码" prop="certificatesnumber">
                <el-input
                  v-model="dataForm.certificatesnumber"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="学历" prop="education">
                <el-select
                  v-model="dataForm.education"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <el-option
                    v-for="(item, index) in educationOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所学专业" prop="major">
                <el-input
                  v-model="dataForm.major"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="工作时间" prop="worktime">
                <el-date-picker
                  v-model="dataForm.worktime"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="timestamp"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="职称/职务" prop="technicaltitle">
                <el-input
                  v-model="dataForm.technicaltitle"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="职称专业" prop="xzjob">
                <el-input
                  v-model="dataForm.xzjob"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="技能等级" prop="skilllevel">
                <el-input
                  v-model="dataForm.skilllevel"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所任课程" prop="teachingsubjects">
                <el-input
                  v-model="dataForm.teachingsubjects"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                label="从事安全生产培训工作时间"
                prop="safeworktime"
              >
                <el-date-picker
                  v-model="dataForm.safeworktime"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="timestamp"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="工作单位" prop="unitname">
                <el-input
                  v-model="dataForm.unitname"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="专职/兼职" prop="worktype">
                <el-select
                  v-model="dataForm.worktype"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <el-option
                    v-for="(item, index) in worktypeOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="邮箱" prop="email">
                <el-input
                  v-model="dataForm.email"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="详细地址" prop="detailaddress">
                <el-input
                  v-model="dataForm.detailaddress"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="讲师类型" prop="persontype">
                <el-select
                  v-model="dataForm.persontype"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <el-option
                    v-for="(item, index) in getDict('headTeacher')"
                    :key="index"
                    :label="item.dataName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="安全培训教师岗位证书编号" prop="fixedphone">
                <el-input
                  v-model="dataForm.fixedphone"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="移动电话" prop="telephone">
                <el-input
                  v-model="dataForm.telephone"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="个人简历" prop="personalresume">
                <el-input
                  v-model="dataForm.personalresume"
                  placeholder="请输入"
                  :style="{ width: '100%' }"
                  true
                  type="textarea"
                  :autosize="{ minRows: 4, maxRows: 4 }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="主要教学科研成果" prop="scientific">
                <el-input
                  v-model="dataForm.scientific"
                  placeholder="请输入"
                  :style="{ width: '100%' }"
                  true
                  type="textarea"
                  :autosize="{ minRows: 4, maxRows: 4 }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remarks">
                <JADP-UploadFz
                  v-model="dataForm.remarks"
                  :fileSize="5"
                  :showTip="true"
                  sizeUnit="MB"
                  :limit="9"
                  buttonText="附件上传"
                >
                </JADP-UploadFz>
              </el-form-item>
            </el-col>
          </template>
        </el-form>
      </el-row>
    </div>
  </transition>
</template>
<script>
import request from "@/utils/request";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
export default {
  components: {},
  props: {
    premium: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        realname: "",
        gender: "",
        birthday: "",
        politicallandscape: "",
        certificatesnumber: "",
        education: "",
        major: "",
        worktime: "",
        technicaltitle: "",
        xzjob: "",
        skilllevel: "",
        teachingsubjects: "",
        safeworktime: "",
        unitname: "",
        worktype: "",
        email: "",
        detailaddress: "",
        fixedphone: "",
        telephone: "",
        personalresume: "",
        scientific: "",
        remarks: [],
        photo: [],
        creatoruserid: "",
        creatortime: "",
        lastmodifyuserid: "",
        lastmodifytime: "",
        persontype: ""
      },
      rules: {
        photo: [
          {
            required: false,
            message: "请选择图片",
            trigger: "blur"
          }
        ],
        realname: [
          {
            required: true,
            message: "请输入",
            trigger: "blur"
          }
        ],
        // certificatesnumber: [
        //   {
        //     required: true,
        //     message: "请输入",
        //     trigger: "blur"
        //   }
        // ]
        // teachingsubjects: [
        //   {
        //     required: true,
        //     message: "请输入",
        //     trigger: "blur"
        //   }
        // ],
        // worktype:[
        //   {
        //     required: true,
        //     message: "请选择",
        //     trigger: "blur"
        //   }
        // ],
      },
      genderOptions: [],
      educationOptions: [],
      worktypeOptions: [
        { fullName: "专职", id: "1" },
        { fullName: "兼职", id: "2" }
      ]
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getgenderOptions();
    this.geteducationOptions();
  },
  mounted() {},
  methods: {
    getgenderOptions() {
      getDictionaryDataSelector("963255a34ea64a2584c5d1ba269c1fe6").then(
        res => {
          this.genderOptions = res.data.list;
        }
      );
    },
    geteducationOptions() {
      getDictionaryDataSelector("6a6d6fb541b742fbae7e8888528baa16").then(
        res => {
          this.educationOptions = res.data.list;
        }
      );
    },
    goBack() {
      this.$emit("refresh");
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/teachersManage/Xy_ks_teachers/" + this.dataForm.id,
            method: "get"
          }).then(res => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate(valid => {
        if (valid) {
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      _data.organizeId = this.$store.getters.organizeId;
      if (!this.dataForm.id) {
        _data.premium = this.premium;
        request({
          url: "/api/teachersManage/Xy_ks_teachers",
          method: "post",
          data: _data
        }).then(res => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            }
          });
        });
      } else {
        request({
          url: "/api/teachersManage/Xy_ks_teachers/" + this.dataForm.id,
          method: "PUT",
          data: _data
        }).then(res => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            }
          });
        });
      }
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.remarks = JSON.stringify(_data.remarks);
      _data.photo = JSON.stringify(_data.photo);
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.remarks = JSON.parse(_dataAll.remarks);
      _dataAll.photo = JSON.parse(_dataAll.photo);
      this.dataForm = _dataAll;
      // this.dataForm.photo = this.dataForm.photo
      //   ? JSON.parse(this.dataForm.photo)
      //   : [];
    }
  }
};
</script>
